package com.lagou.java.intercepter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
自定义springmvc拦截器
 */
public class MyIntercepter implements HandlerInterceptor {

    /**
     * 会在hander方法业务逻辑之前执行
     *  往往在这里完成全权限校验
     * @param request
     * @param response
     * @param handler
     * @return 返回值Boolean代表是否放行, true代表放行, false代表中止
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyIntercepter preHandle....");
        return true;
    }

    /**
     * 会在handle业务方法执行完毕之后尚未跳转页面是执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView 封装了视图和数据, 此时尚未跳转页面, 你可以在这里针对返回的数据和视图信息进行更改
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyIntercepter postHandle...");

    }

    /**
     * 页面已经跳转, 渲染完成之后执行
     * @param request
     * @param response
     * @param handler
     * @param ex 在这里可以捕获异常
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyIntercepter afterCompletion...");

    }
}
